package org.game.module.event.listener;

import java.util.List;

import org.apache.commons.collections4.MapUtils;
import org.constant.EventConditionKey;
import org.constant.EventConditionType;
import org.constant.EventType;
import org.game.db.bean.Player;
import org.game.db.bean.Role;
import org.game.db.cache.RoleCache;
import org.game.plugin.event.GameEvent;
import org.game.plugin.event.GameEventListener;
import org.game.plugin.event.GameEventStack;
import org.game.plugin.event.GameEventType;

import com.sojoys.artifact.tools.ToolMap;

/**
 * @author : DengYing
 * @CreateDate : 2018年1月11日 下午2:59:44
 * @Description ：精灵事件
 */
@GameEventType(EventType.ROLE_CHANGE)
public class RoleEventListenner implements GameEventListener {

	@Override
	public void onEvent(GameEvent event) {
		Player player = event.getPlayer();
		GameEventStack eventStack = event.peek();
		switch (eventStack.getConditionType()) {
		case EventConditionType.FINISH_DATING_COUNT: {
			if (checkTriggerType(event.getIn(), eventStack.getConditionType())){
				boolean bool = verify_eq(EventConditionKey.ROLE_CID, event.getIn(), eventStack.getParams());
				bool &= verify_eq(EventConditionKey.DATING_TYPE, event.getIn(), eventStack.getParams());
				bool &= verify_eq(EventConditionKey.SCRIPT_ID, event.getIn(), eventStack.getParams());
				bool &= verify_eq(EventConditionKey.END_TYPE, event.getIn(), eventStack.getParams());
				if (bool) {
					eventStack.changeProgress(1);
				}
			}

			break;
		}
        case EventConditionType.DONATE_GIFT:{
        	if (checkTriggerType(event.getIn(), eventStack.getConditionType())) {
				boolean bool = verify_eq(EventConditionKey.ROLE_CID, event.getIn(), eventStack.getParams());
				bool &= verify_eq(EventConditionKey.GIFT_CID, event.getIn(), eventStack.getParams());
				
				boolean checkCount = false;
				if(ToolMap.getInt(EventConditionKey.MAX_COUNT,eventStack.getParams(),0) > 0){
					checkCount = checkTriggerCount(player, event, eventStack);		
					bool &= checkCount;
				}

				if (bool) {
					eventStack.changeProgress(1);
					if (checkCount) {
						updateDonate(player, event);						
					}
				}
			}
			break;
        }
        case EventConditionType.ROLE_FAVOR:{
        	if (checkTriggerType(event.getIn(), eventStack.getConditionType())){
				boolean bool = verify_eq(EventConditionKey.ROLE_CID, event.getIn(), eventStack.getParams());
				int oldFavor = ToolMap.getInt(EventConditionKey.OLD_FAVOR, event.getIn());
				int nowFavor = ToolMap.getInt(EventConditionKey.NOW_FAVOR, event.getIn());
				int targetFavor = ToolMap.getInt(EventConditionKey.FAVOR,eventStack.getParams());
				if (bool && oldFavor < targetFavor && targetFavor <= nowFavor) {
					eventStack.setProgress(nowFavor>targetFavor?targetFavor:nowFavor);
				}
			}
            break;
        }
        case EventConditionType.ROLE_FAVOR_COUNT:{
        	if (checkTriggerType(event.getIn(), EventConditionType.ROLE_FAVOR)){
				int oldFavor = ToolMap.getInt(EventConditionKey.OLD_FAVOR, event.getIn());
				int nowFavor = ToolMap.getInt(EventConditionKey.NOW_FAVOR, event.getIn());
				boolean bool = verify_eq(EventConditionKey.ROLE_CID, event.getIn(), eventStack.getParams());

				int limit = ToolMap.getInt(EventConditionKey.LIMIT, eventStack.getParams());
				if (bool && oldFavor < limit && nowFavor >= limit) {
					eventStack.changeProgress(1);
				}
			}
            break;
        }
		}
		
	}

	@Override
	public void initEventCondition(Player player, GameEventStack eventStack) {
		switch (eventStack.getConditionType()) {
        case EventConditionType.ROLE_FAVOR:{
        	List<Role> roles = player.getRolesFkPlayerId();
        	int maxFavor = 0;
        	for (Role role : roles) {
        		int roleId = ToolMap.getInt(EventConditionKey.ROLE_CID, eventStack.getParams());
        		if (roleId != 0 && roleId != role.getCid()) {
					continue;
				}
        		maxFavor = Math.max(maxFavor, role.getFavor());
			}
			eventStack.setProgress(maxFavor);
            break;
        }
        case EventConditionType.ROLE_FAVOR_COUNT:{
        	List<Role> roles = player.getRolesFkPlayerId();
        	int count = 0;
        	for (Role role : roles) {
        		int roleId = ToolMap.getInt(EventConditionKey.ROLE_CID, eventStack.getParams());
        		int limit = ToolMap.getInt(EventConditionKey.LIMIT, eventStack.getParams());
        		if (roleId != 0 && roleId != role.getCid()) {
					continue;
				}
        		if (limit != 0 && role.getFavor() < limit) {
					continue;
				}
        		count++;
			}
			eventStack.setProgress(count);
            break;
        }
		}
	}
	
	private boolean checkTriggerCount(Player player, GameEvent event, GameEventStack eventStack) {
		int maxCount = ToolMap.getInt(EventConditionKey.MAX_COUNT,eventStack.getParams(),0);

		boolean bool = MapUtils.getBoolean(event.getFirstMap(), "checkTriggerCount",false);
		if (bool) {
			return bool;
		}
		
        Role role = RoleCache.me().getByCidPlayerId(ToolMap.getInt(EventConditionKey.ROLE_CID, event.getIn(),0), player.getId());
        int giftId = ToolMap.getInt(EventConditionKey.GIFT_CID, event.getIn(),0);
        int count = (int) role.getDonateDating().computeIfAbsent(giftId, k -> 0);

        
        bool = count < maxCount;
        if (bool) {
        	event.getFirstMap().put("checkTriggerCount", true);
		}
        return bool;
    }

    private void updateDonate(Player player, GameEvent event){
    	
    	boolean bool = MapUtils.getBoolean(event.getFirstMap(), "updateDonate",false);
		if (bool) {
			return;
		}
		
		// 修改触发记录
		Role role = RoleCache.me().getByCidPlayerId((Integer) event.getIn().get(EventConditionKey.ROLE_CID), player.getId());
		int giftId = (Integer) event.getIn().get(EventConditionKey.GIFT_CID);
		int count = (int) role.getDonateDating().computeIfAbsent(giftId, k -> 0);
		role.getDonateDating().put(giftId, count + 1);
		role.update();

		
		event.getFirstMap().put("updateDonate", true);
	}
}
